package com.gomore.opple.service;

import android.os.AsyncTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * Company 上海勾芒科技有限公司.
 */
public class DownloadApkTask extends AsyncTask<String, Integer, Map<String, String>> {

    private final String STATE = "state";
    private final String FILE_PATH = "file_path";

    DownloadApkProcessor downloadApkProcessor;

    public void setDownloadApkProcessor(DownloadApkProcessor downloadApkProcessor) {
        this.downloadApkProcessor = downloadApkProcessor;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        downloadApkProcessor.updateDownloadProgress(values[0], values[1]);
        super.onProgressUpdate(values);
    }

    @Override
    protected HashMap<String, String> doInBackground(String... params) {
        String url = params[0];
        String path = params[1];

        HashMap<String, String> map = new HashMap<String, String>();
        map.put(FILE_PATH, path);

        Integer downloadedSize = 0;
        Integer provalue = 0;
        try {
            HttpGet httpGet = new HttpGet(url.trim());
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                InputStream is = httpResponse.getEntity().getContent();
                long sizeoffile = httpResponse.getEntity().getContentLength();
                if (sizeoffile < 0) {
                    map.put(STATE, String.valueOf(false));
                } else {
                    FileOutputStream fos = new FileOutputStream(path);
                    byte[] buffer = new byte[4096];
                    int count = 0;
                    while ((count = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, count);
                        downloadedSize += count;
                        provalue = (int) (downloadedSize * 100.0 / sizeoffile);
                        publishProgress(downloadedSize, (int) sizeoffile);
                    }
                    fos.flush();
                    fos.close();
                    is.close();
                    if (provalue >= 100) {
                        map.put(STATE, String.valueOf(true));
                    } else {
                        map.put(STATE, String.valueOf(false));
                    }
                }
            }
        } catch (Exception e) {
            map.put(STATE, String.valueOf(false));
            e.printStackTrace();
        }
        return map;
    }

    @Override
    protected void onPostExecute(Map<String, String> map) {
        super.onPostExecute(map);
        String filePath = map.get(FILE_PATH);
        String state = map.get(STATE);
        if (String.valueOf(true).equals(state)) {
            downloadApkProcessor.onDownloadCompleted(filePath);
        } else {
            downloadApkProcessor.onDownloadFailed();
        }
    }
}
